import multiprocessing
import psutil
import asyncio

def fibonacci(n):
    """
    计算第n个斐波那契数列
    """
    if n <= 0:
        return []
    elif n == 1:
        return [0]
    elif n == 2:
        return [0, 1]
    else:
        fib_list = [0, 1]
        while len(fib_list) < n:
            new_num = fib_list[-1] + fib_list[-2]
            fib_list.append(new_num)
        return fib_list

def worker(num):
    """
    一个简单的工作函数，这里可以替换为你的计算密集型任务
    """
    print(f"Worker {num} is running")
    # 模拟工作
    for i in range(20000):
        array=fibonacci(8000)
        array2=fibonacci(4000)
        print(len(array), end="")
        print(len(array2))


async def wait_for_a_minute():
    print("开始等待2分钟....")
    await asyncio.sleep(120)
    print("已经等待了2分钟。")
    return "Done"


def improve_cpu():
    # 获取CPU核心数
    num_cpus = multiprocessing.cpu_count()
    print(f"Using {num_cpus} CPUs")

    # 创建进程池
    pool = multiprocessing.Pool(processes=num_cpus)

    # 向进程池提交任务
    for i in range(num_cpus):
        pool.apply_async(worker, args=(i,))

    # 关闭进程池并等待所有任务完成
    pool.close()
    pool.join()
    print("CPU测试完成")


def improve_memory():
    total_memory = psutil.virtual_memory().total / 1024 ** 3
    print("总内存" + str(total_memory) + "GB")
    free_memory = psutil.virtual_memory().free / 1024 ** 3
    print("可用内存" + str(free_memory) + "GB")
    # 使用剩余内存60%
    use_size = int(psutil.virtual_memory().free * 6 / 10)
    print("预计使用" + str(use_size / 1024 ** 3) + "GB")
    print("开始内存测试")
    memory = bytearray(use_size)
    asyncio.run(wait_for_a_minute())
    print(len(memory))
    print("内存测试完成")

if __name__ == "__main__":
    improve_cpu()
    improve_memory()

